package com.xmbandu.app.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;

public class DensityUtil {

    // 根据手机的分辨率将dp的单位转成px(像素)
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    // 根据手机的分辨率将px(像素)的单位转成dp
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    // 将px值转换为sp值
    public static int px2sp(Context context, float pxValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }

    // 将sp值转换为px值
    public static int sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    // 屏幕宽度（像素）
    public static int getWindowWidth(Activity context) {
        DisplayMetrics metric = new DisplayMetrics();
        context.getWindowManager().getDefaultDisplay().getMetrics(metric);
        return metric.widthPixels;
    }

    // 屏幕宽度（像素）
    public static int getWindowWidth(Context context) {
        WindowManager manager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        Display display = manager.getDefaultDisplay();
        return display.getWidth();
    }

    // 屏幕高度（像素）
    public static int getWindowHeight(Activity activity) {
        DisplayMetrics metric = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metric);
        return metric.heightPixels;
    }

    /**
     * @param
     * @return
     * @throws Exception
     * @author zhx
     */
    public static Point getScreenMetrics(Context context) {
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        int w_screen = dm.widthPixels;
        int h_screen = dm.heightPixels;
        Log.i("Util", "Screen---Width = " + w_screen + " Height = " + h_screen
                + " densityDpi = " + dm.densityDpi);
        return new Point(w_screen, h_screen);

    }

    public static Rect createCenterScreenRect(Context context, Rect rect) {
        int x1 = dip2px(context, rect.left);
        int y1 = dip2px(context, rect.top);
        int x2 = getScreenMetrics(context).x
                - dip2px(context, rect.right);
        int y2 = getScreenMetrics(context).y
                - dip2px(context, rect.bottom);
//        int width = x2 - rect.left;
//        int height = y2 - rect.top;
        Log.i("Util",  "@=getScreenMetrics(context).x==" +getScreenMetrics(context).x + "--@getScreenMetrics(context).y--" + getScreenMetrics(context).y );
        Log.i("Util",  "@=rect.right==" +dip2px(context, rect.right) + "--@rect.bottom--" + dip2px(context, rect.bottom) );
//        Log.i("Util", x1 + "@" + y1 + "@" + x2 + "@" + y2);
//        if (width <= height) {
            y2 = y1 + x2-x1;
//        } else {
//            x2 = x1 + height;
//        }
        Log.i("Util", x1 + "@" + y1 + "@" + x2 + "@" + y2);
        return new Rect(x1, y1, x2, y2);
    }

}
